const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js');
const nav = require('../../utils/navigator.js');
const util = require('../../utils/util.js');
//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryParentList: {}, //品种-父类
    categoryParentChildList: {}, //品种-子类
    selected_categoryParent: -1, //
    selected_categoryChild: {}, //
    currentPz: '品种',
    callBackPage:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({ 
      callBackPage: options.callBackPage == null ? {} : options.callBackPage,
      
      });
    //加载品种列表
    http.post(
      appConfig.baseUrl + 'Notice/GetCategoryListByParentId', {
        ParentId: 0
      },
      function (res) {
        that.setData({
          categoryParentList: res.data
        });
      });
  },
  sureSelectCategoryListener:function(e){
    console.log(e.detail.currentTarget.dataset);

    switch(e.detail.currentTarget.dataset.callbackpage)
    {
      case "index":
        nav.redirectTo("../index/index?selected_categoryChild=" + e.detail.currentTarget.dataset.id + "&currentPz=" + e.detail.currentTarget.dataset.categoryname);
        break;
      case "category":
        // nav.redirectTo("../notice/notice?selectedCategoryChild=" + e.detail.currentTarget.dataset.id + "&selectedCategoryParent=" + e.detail.currentTarget.dataset.categoryparent);
        var id=e.detail.currentTarget.dataset.categoryparent;
        http.post(
          appConfig.baseUrl + 'Notice/GetCategoryById', {
            Id: id
          },
          function (res) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({
              selectedCategoryParent: res.data.Name,
              selectedCategoryChild:e.detail.currentTarget.dataset.categoryname
            });
            wx.navigateBack({
                delta: 1,
            });
          });
        
        break;
    }
  },
  // selectCategoryParent: function (e) {
  //   var that = this;
  //   that.setData({
  //     selected_categoryParent: e.currentTarget.dataset.category,
  //     categoryParentChildList:{}
  //   });
  //   http.post(
  //     appConfig.baseUrl + 'Notice/GetCategoryListByParentId', {
  //       ParentId: e.target.dataset.category
  //     },
  //     function (res) {
  //       console.log(res.data)
  //       that.setData({
  //         categoryParentChildList: res.data
  //       });
  //     });
  // },
  // selectCategoryChild: function (e) {
  //   console.log(e.currentTarget.dataset.category);
  //   var that = this;
  //   nav.redirectTo("../index/index?selected_categoryChild=" + e.currentTarget.dataset.category + "&currentPz=" + e.currentTarget.dataset.categoryName);
  // },
  // selectAllCategory: function (e) {
  //   var that = this;
  //   that.setData({
  //     selected_categoryParent: -1,
  //     categoryParentChildList: {},
  //     currentPz: '全部'
  //   });
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})